//
//  EYProtocol.m
//  metadata
//
//  Created by easy on 11-5-27.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EYProtocol.h"


@implementation EYProtocol
@synthesize ownProtocol = own; 

+(id) init:(Protocol *) protocol{
	return [[[EYProtocol alloc] initWith:protocol] autorelease];
}
-(id) initWith:(Protocol *)protocol{
	if (self = [super init]) {
		own = protocol;
	}
	return self;
}
-(BOOL) conforms:(EYProtocol *) p{
	return [EYProtocol conforms:self to:p];
}
+(BOOL) conforms:(EYProtocol *) p1 to:(EYProtocol *) p2{
	return protocol_conformsToProtocol(p1.ownProtocol, p2.ownProtocol);
}

- (EYProperty *) propertyWithName:(NSString *) propertyName isRequiredProperty:(BOOL) req isInstanceProperty:(BOOL) ins{
	objc_property_t prop = protocol_getProperty(self.ownProtocol, [propertyName cStringUsingEncoding:DEFAULT_ENCODING], req, ins);
	return [EYProperty init:prop];
}
-(struct objc_method_description) methodDescription:(SEL) selector isRequiredMethod:(BOOL) req isInstanceMethod:(BOOL) ins{
	return protocol_getMethodDescription(self.ownProtocol, selector, req, ins);
}

+(BOOL) isEquals:(EYProtocol *) protocol1 to:(EYProtocol *) protocol2{
	return protocol1 != nil && protocol2 != nil && protocol_isEqual(protocol1.ownProtocol, protocol2.ownProtocol);
}
-(BOOL) isEquals:(EYProtocol *) protocol{
	return [EYProtocol isEquals:self to:protocol];
}
-(NSArray /*EYProtocol*/*) protocolList{
	unsigned int outCount = 0;
	Protocol **ps = protocol_copyProtocolList(self.ownProtocol, &outCount);
	NSMutableArray *arr = [NSMutableArray arrayWithCapacity:outCount];
	if (outCount > 0) {
		for (int i = 0; i < outCount; i++) {
			Protocol *pl = *(ps + i);
			EYProtocol *p = [[EYProtocol alloc] initWith:pl];
			[arr addObject:p];
			[p release];
			[*(ps+i) release];
		}
	}
	ps = nil;
	return arr;
}
-(NSArray /*EYProperty*/*) propertyList{
	unsigned int outCount = 0;
	objc_property_t *props = protocol_copyPropertyList(self.ownProtocol, &outCount);
	NSMutableArray *arr = [NSMutableArray arrayWithCapacity:outCount];
	if (outCount > 0) {
		for (int i = 0; i < outCount; i++) {
			EYProperty *p = [[EYProperty alloc] initWith:*(props + i)];
			[arr addObject:p];
			[p release];
		}
	}
	props = nil;
	return arr;
}
-(struct objc_method_description *) methodDescriptionList :(BOOL)isRequiredProperty isInstanceProperty:(BOOL) ins outCount:(unsigned int) outCount{
	return protocol_copyMethodDescriptionList(self.ownProtocol, isRequiredProperty, ins, &outCount);
}
-(void) dealloc{
	free(own);
	[super dealloc];
}
#pragma mark -
#pragma mark setter/getter
 
-(NSString *) name{
	return [NSString stringWithCString:protocol_getName(self.ownProtocol) encoding:DEFAULT_ENCODING];
}
@end
